package com.cuckoo.controller;

import com.cuckoo.common.JsonData;
import com.cuckoo.pojo.AuthCode;
import com.cuckoo.pojo.User;
import com.cuckoo.service.AuthCodeService;
import com.cuckoo.service.UserService;
import com.cuckoo.util.MD5;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * UserController
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AuthCodeService authCodeService;

    /**
     * 默认进入登录的首页
     * @return
     */
    @RequestMapping("/")
    public String login(){
        return "user/login";
    }

    /**
     * 注册
     * @return
     */
    @RequestMapping("/register")
    public String register(){
        return "user/register";
    }


    /**
     * 登录
     * @return
     */
    @RequestMapping(value = "/login",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public JsonData login_to(String info,String password){
        try {
            UsernamePasswordToken token= new UsernamePasswordToken ( info, MD5.md5 ( password ) );
            SecurityUtils.getSubject ().login ( token );
            Subject subject = SecurityUtils.getSubject();
            User currentUser= (User) subject.getPrincipal ();
            return JsonData.success (currentUser);
        } catch (Exception e) {
            return JsonData.error ( "用户名或者密码错误" );
        }
    }




    /**
     * 增加一个用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/add_user",produces ="application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public JsonData addUser(User user,String code){
        try {
            Integer result=userService.addUser ( user,code);
            if (result>0){
                return JsonData.success ( result );
            }
            return JsonData.error ( "请联系管理员");
        } catch (Exception e) {
            return JsonData.error ( e.getMessage ());
        }
    }

    /**
     * 发送短信（聚合提供数据支持）
     * @return
     */
    @RequestMapping(value = "/sms_code",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public JsonData SMS(String phone,Integer type){
        try{
            authCodeService.addAuth ( phone,type);
            return JsonData.success ();
        }catch (Exception e){
            return JsonData.error ( e.getMessage () );
        }
    }


    /**
     * 验证手机是否存在
     * @param phone
     * @return
     */
    @RequestMapping(value = "/auth_phone",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public JsonData authPhone(String phone){
        //判断此手机是否存在
        User user=userService.findUserByNameOrPhone ( phone );
        if (user!=null){
            //已经存在
            return JsonData.error ( "此手机已经存在" );
        }
        return JsonData.success ();
    }

}
